﻿using System;
using System.Windows;
using System.Windows.Controls;

namespace AIPASoft.CommonLibrary.WPF.MVVM
{
    /// <summary>
    /// Provides interface to bind Password property through xaml
    /// </summary>
    public static class PasswordHelper
    {
        #region Attach Property

        public static readonly DependencyProperty AttachProperty =
            DependencyProperty.RegisterAttached("Attach", typeof(bool), typeof(PasswordHelper),
            new PropertyMetadata(default(bool), OnAttachPropertyChanged));

        public static void SetAttach(UIElement element, bool value)
        {
            element.SetValue(AttachProperty, value);
        }

        public static bool GetAttach(UIElement element)
        {
            return (bool)element.GetValue(AttachProperty);
        }

        private static void OnAttachPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var passwordBox = d as PasswordBox;
            if (passwordBox == null)
            {
                return;
            }

            //Use Generic WeakEventManager<T,EventArgs> to avoid memory leak
            //C# 4.5 new feature
            if ((bool)e.OldValue)
            {
                //Unsubscribe PasswordChanged event
                WeakEventManager<PasswordBox, RoutedEventArgs>
                    .RemoveHandler(passwordBox, PasswordBox.PasswordChangedEvent.Name,PasswordChangedEventHandler);
            }

            if ((bool)e.NewValue)
            {
                //Subscribe PasswordChanged event
                WeakEventManager<PasswordBox, RoutedEventArgs>.
                    AddHandler(passwordBox, PasswordBox.PasswordChangedEvent.Name, PasswordChangedEventHandler);
            }

        }

        #endregion

        #region Password Property

        public static readonly DependencyProperty PasswordProperty =
            DependencyProperty.RegisterAttached("Password", typeof(string), typeof(PasswordHelper),
            new PropertyMetadata(default(string), OnPasswordPropertyChanged));


        public static void SetPassword(UIElement element, string value)
        {
            element.SetValue(PasswordProperty, value);
        }

        public static string GetPassword(UIElement element)
        {
            return (string)element.GetValue(PasswordProperty);
        }

        private static void PasswordChangedEventHandler(object sender, RoutedEventArgs eventArgs)
        {
            var passwordBox = sender as PasswordBox;
            if (passwordBox == null)
            {
                return;
            }

            SetPassword(passwordBox, passwordBox.Password);
        }
        #endregion

        /// <summary>
        /// PasswordBox.PasswordChangedevent
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void OnPasswordPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var passwordBox = sender as PasswordBox;
            if (passwordBox == null)
            {
                return;
            }

            passwordBox.Password = Convert.ToString(e.NewValue);
        }

    }
}
